﻿using System.Text;
using Sanelib.Common.SystemSettings;

namespace Sanelib.CodeMaker.Generators
{
    public class ViewGenerator
    {
        public static void Generate(DatabaseTableSchema tbSchema)
        {
            var sb = new StringBuilder();

            sb.AppendLine("using System;");
            sb.AppendLine("using PetaPoco;");
            sb.AppendLine("using Sanelib.DataView;");
            sb.AppendLine("");
            sb.AppendLine("namespace Common.Views");
            sb.AppendLine("{");
            sb.AppendLine("\t[TableName(\"" + tbSchema.TableName + "View\")]");

            sb.AppendLine("\tpublic class " + tbSchema.TableName + "View : " + (tbSchema.IsAudited? "Audited" : "") + "View");
            sb.AppendLine("\t{");
            sb.AppendLine("");

            foreach (var item in tbSchema.Columns)
            {
                sb.AppendLine(item.GetPropertyDeclaration("\t\t", true));
            }

            sb.AppendLine("");
            sb.AppendLine("\t}");
            sb.AppendLine("}");

            System.IO.File.WriteAllText(Globals.CreateOrGetCustomPath("..\\Common\\Views\\") + tbSchema.TableName + "View_Generated.cs", sb.ToString());
        }
    }
}
